/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ExtractPeliculasFrame.java
 *
 * Created on 20-feb-2011, 21:10:15
 */
package GUI;

import data.ExtractorPeliculasCinetube;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.util.Date;
import javax.swing.DefaultComboBoxModel;

/**
 *
 * @author user
 */
public class ExtractPeliculasFrame extends javax.swing.JFrame {

    /** Creates new form ExtractPeliculasFrame */
    public ExtractPeliculasFrame() {
        initComponents();
        centrarVentanas();
        resetLogFile();
    }

    private void resetLogFile() {
        FileWriter fichero = null;
        PrintWriter pw = null;
        try {
            fichero = new FileWriter("./log.txt");
            pw = new PrintWriter(fichero);
            pw.write("El proceso comenzó a " + new Date() + "\n");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // Nuevamente aprovechamos el finally para
                // asegurarnos que se cierra el fichero.
                if (null != fichero) {
                    fichero.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
    }

    private void rellenarHastaComboBox(String letra) {
        DefaultComboBoxModel model = new DefaultComboBoxModel();
        char letraInicial;
        if (letra.equalsIgnoreCase("0-9")) {
            model.addElement("0-9");
            letraInicial = 'A';
        } else {
            letraInicial = letra.charAt(0);
        }
        for (char letraAux = letraInicial; letraAux <= 'Z'; letraAux++) {
            model.addElement("" + letraAux);
        }
        hastaComboBox.setModel(model);
        this.repaint();
    }

    private void centrarVentanas() {
        // Se obtienen las dimensiones en pixels de la pantalla.
        Dimension pantalla = Toolkit.getDefaultToolkit().getScreenSize();
        // Se obtienen las dimensiones en pixels de la ventana.
        Dimension ventana = this.getSize();
        // Una cuenta para situar la ventana en el centro de la pantalla.
        this.setLocation((pantalla.width - ventana.width) / 2, (pantalla.height - ventana.height) / 2);

        this.setVisible(true);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        fondoPanel1 = new javax.swing.JPanel();
        desdeLabel1 = new javax.swing.JLabel();
        DesdeComboBox = new javax.swing.JComboBox();
        hastaLabel1 = new javax.swing.JLabel();
        hastaComboBox = new javax.swing.JComboBox();
        descargarButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jPanel1.setBackground(new java.awt.Color(51, 51, 51));

        fondoPanel1.setBackground(new java.awt.Color(26, 23, 23));

        desdeLabel1.setFont(new java.awt.Font("Berlin Sans FB", 0, 18));
        desdeLabel1.setForeground(new java.awt.Color(51, 102, 255));
        desdeLabel1.setText("Descargar películas desde");

        DesdeComboBox.setFont(new java.awt.Font("Berlin Sans FB", 0, 18));
        DesdeComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0-9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }));
        DesdeComboBox.setOpaque(false);
        DesdeComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                DesdeComboBoxActionPerformed(evt);
            }
        });

        hastaLabel1.setFont(new java.awt.Font("Berlin Sans FB", 0, 18));
        hastaLabel1.setForeground(new java.awt.Color(51, 102, 255));
        hastaLabel1.setText("Hasta");

        hastaComboBox.setFont(new java.awt.Font("Berlin Sans FB", 0, 18));
        hastaComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "0-9", "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" }));
        hastaComboBox.setOpaque(false);

        descargarButton.setBackground(new java.awt.Color(102, 102, 102));
        descargarButton.setFont(new java.awt.Font("Berlin Sans FB", 0, 18));
        descargarButton.setForeground(new java.awt.Color(51, 153, 255));
        descargarButton.setText("Descargar!");
        descargarButton.setOpaque(false);
        descargarButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                descargarButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout fondoPanel1Layout = new javax.swing.GroupLayout(fondoPanel1);
        fondoPanel1.setLayout(fondoPanel1Layout);
        fondoPanel1Layout.setHorizontalGroup(
            fondoPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fondoPanel1Layout.createSequentialGroup()
                .addGroup(fondoPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(fondoPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(descargarButton))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, fondoPanel1Layout.createSequentialGroup()
                        .addGap(54, 54, 54)
                        .addGroup(fondoPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(hastaLabel1)
                            .addComponent(desdeLabel1))
                        .addGap(18, 18, 18)
                        .addGroup(fondoPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(hastaComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(DesdeComboBox, 0, 56, Short.MAX_VALUE))))
                .addContainerGap(75, Short.MAX_VALUE))
        );
        fondoPanel1Layout.setVerticalGroup(
            fondoPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(fondoPanel1Layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addGroup(fondoPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(DesdeComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(desdeLabel1))
                .addGap(18, 18, 18)
                .addGroup(fondoPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(hastaLabel1)
                    .addComponent(hastaComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 21, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(descargarButton, javax.swing.GroupLayout.DEFAULT_SIZE, 36, Short.MAX_VALUE)
                .addGap(21, 21, 21))
        );

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(fondoPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(38, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addComponent(fondoPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(22, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void DesdeComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_DesdeComboBoxActionPerformed
        rellenarHastaComboBox(DesdeComboBox.getModel().getSelectedItem().toString());
}//GEN-LAST:event_DesdeComboBoxActionPerformed

    private void descargarButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_descargarButtonActionPerformed
        String letraInicial = DesdeComboBox.getModel().getSelectedItem().toString();
        String letraFinal = hastaComboBox.getModel().getSelectedItem().toString();
        new ExtractorPeliculasCinetube().extract(letraInicial, letraFinal);
}//GEN-LAST:event_descargarButtonActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ExtractPeliculasFrame().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox DesdeComboBox;
    private javax.swing.JButton descargarButton;
    private javax.swing.JLabel desdeLabel1;
    private javax.swing.JPanel fondoPanel1;
    private javax.swing.JComboBox hastaComboBox;
    private javax.swing.JLabel hastaLabel1;
    private javax.swing.JPanel jPanel1;
    // End of variables declaration//GEN-END:variables
}
